﻿using B.MES.FLH.WarehouseManagement.Domain.Entity;
using B.MES.SLY.Domain.Entities;
using Infrastructure;
using Microsoft.AspNetCore.Http;
using Standard;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WorkflowCore2210A.Application.Dto;
using YSL.Application.Ysl_Dto.Input;
using YSL.Application.Ysl_Dto.定时任务DTO;
using YSL_Domain.Model;

namespace YSL.Application.Services.定时任务
{
    public class HangfireService:IHangfireService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly MesRepository<SLY_CUSTOMER_MANAGEMENTMODEL> _customerRepository;
        private readonly MesRepository<IOWAREHOUSERECORDMODEL> _ioWarehouseRecordRepository;
        private readonly MesRepository<WAREHOUSEDEFINITIONMODEL> _warehouseDefinitionRepository;
        private readonly MesRepository<YSL_WORKORDER> _workOrderListRepository;

        public HangfireService(IHttpContextAccessor httpContextAccessor, MesRepository<SLY_CUSTOMER_MANAGEMENTMODEL> customerRepository, MesRepository<IOWAREHOUSERECORDMODEL> ioWarehouseRecordRepository, MesRepository<WAREHOUSEDEFINITIONMODEL> warehouseDefinitionRepository, MesRepository<YSL_WORKORDER> workOrderListRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _customerRepository = customerRepository;
            _ioWarehouseRecordRepository = ioWarehouseRecordRepository;
            _warehouseDefinitionRepository = warehouseDefinitionRepository;
            _workOrderListRepository = workOrderListRepository;
        }
        /// <summary>
        /// 获取客户数、出入库记录数、仓库总数
        /// </summary>
        public Standard.ApiResult<HAngfireDto> RefreshAllDataAsync(HangfirePageInput input)
        {
           // 1.工单分月统计
                var workOrderList = _workOrderListRepository.GetQueryable(x => x.ISDELETE == false && x.LOCK_STATUE == "已解锁" && x.START_TIME != null)
                    .Select(x => new { x.START_TIME, x.NUMBER_COL }).OrderByDescending(x=>x.START_TIME )
                    .ToList();
             
            var getWorkOrderDateHangfireDtos = workOrderList
                .GroupBy(x => new { Year = x.START_TIME.Value.Year, Month = x.START_TIME.Value.Month })
                .Select(g => new GetWorkOrderDateHangfireDto
                {
                    START_TIME = new DateTime(g.Key.Year, g.Key.Month, 1),
                    TotalNUMBER_COL = g.Sum(x => x.NUMBER_COL ?? 0)
                })
                .OrderBy(x => x.START_TIME)
                .ToList();

            // 2. 工单列表
            var workOrderEntities = _workOrderListRepository.GetQueryable(x => x.ISDELETE == false && x.LOCK_STATUE == "已解锁").OrderByDescending(x => x.CODEID).Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize).ToList();
            var getWorkOrderListDtos = workOrderEntities.Select(x => new GetWorkOrderListDto
            {
                CodeId = x.CODEID,
                WorkOrderType = x.WORKORDER_TYPE,
                ProcessProgress = x.PROCESS_PROGRESS,
                NumberCol = Convert.ToInt32(x.NUMBER_COL ?? 0)
            }).ToList();

            // 3. 客户信息
            var customerEntities = _customerRepository.GetQueryable(x => x.ISDELETE == false).OrderByDescending(x => x.ID).Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize).ToList();
            var customerDtos = customerEntities.Select(x => new CustomerDto
            {
                customeR_NAME = x.CUSTOMER_NAME,
                customeR_NODE = x.CUSTOMER_NODE,
                crediT_CODE = x.CREDIT_CODE
            }).ToList();

            // 4. 总数信息
            var totalWorkOrderNumber = workOrderEntities.Count;
            var totalCustomerNumber = customerEntities.Count;
            var totalIoWarehouseRecordNumber = _ioWarehouseRecordRepository.GetQueryable(x => !x.ISDELETE).Count();
            var totalWarehouseDefinitionNumber = _warehouseDefinitionRepository.GetQueryable(x => !x.ISDELETE).Count();

            var getTotalNumberDto = new GetTotalNumberDTo
            {
                TotalWORKORDERNUMBER = totalWorkOrderNumber,
                TotalCUSTOMERNUMBER = totalCustomerNumber,
                TotalIOWAREHOUSERECORDNumber = totalIoWarehouseRecordNumber,
                TotalWAREHOUSEDEFINITIONNumber = totalWarehouseDefinitionNumber
            };

            // 5. 组装HAngfireDto
            var hangfireDto = new HAngfireDto
            {
                GetWorkOrderDateHangfireDtos = getWorkOrderDateHangfireDtos,
                GetWorkOrderListDtos = getWorkOrderListDtos,
                CustomerDtos = customerDtos,
                GetTotalNumberDTos = new List<GetTotalNumberDTo> { getTotalNumberDto }
            };
            return new Standard.ApiPaging<HAngfireDto>
            {
                Code = ApiCode.Success,
                Message = "所有数据获取成功",
                PageDatas = new List<HAngfireDto> { hangfireDto }
               
            };
        }




    }
}

